<template>
  <div class="weekly_plan_manage_planner">
    <one-title title="周计划管理" @refreshData="refresh">
      <div class="mes_btn">
        <el-tooltip
          effect="dark"
          :content="deleteBtnStatus.content"
          :disabled="!deleteBtnStatus.status"
          popper-class="btn_tooltip"
          placement="bottom">
          <el-button
            v-btn-permission="'del'"
            @click="handleDelete" :class="{'is-disabled':deleteBtnStatus.status}">
            <i class="iconfont icon-btn_delete"></i>
            删除
          </el-button>
        </el-tooltip>
        <el-tooltip
          effect="dark"
          :content="eidtBtnStatus.content"
          :disabled="!eidtBtnStatus.status"
          popper-class="btn_tooltip"
          placement="bottom">
          <el-button
            v-btn-permission="'edit'"
            @click="handUpdate" :class="{'is-disabled':eidtBtnStatus.status}">
            <i class="iconfont icon-btn_edit"></i>
            修改
          </el-button>
        </el-tooltip>
        <el-tooltip
          effect="dark"
          :content="submitBtnStatus.content"
          :disabled="!submitBtnStatus.status"
          popper-class="btn_tooltip"
          placement="bottom">
          <el-button
            v-btn-permission="'submit'"
            type="primary" @click="handleSubmit" :class="{'is-disabled':submitBtnStatus.status}">
            <i class="iconfont icon-tijiao"></i>
            提交
          </el-button>
        </el-tooltip>
        <el-button
          v-btn-permission="'add'"
          type="success" @click="handleAddPlan">
          <i class="iconfont icon-btn_addnew"></i>
          新增
        </el-button>
      </div>
    </one-title>
    <div class="fixed_top_56">
      <mes-search-comp
        ref="mesSearch"
        :searchList="tableColConfig"
        :searchResult="searchCount"
        @searchInfo="_info">
      </mes-search-comp>
      <div class="table_container">
        <el-table
          :data="listData.list"
          ref="tableNode"
          v-loading="isLoading"
          :max-height="tableMaxHeight"
          :row-class-name="({row}) => !selectItems.some((item) => item.id === row.id) || 'line_selected'"
          @row-click="(row) => $refs.tableNode.toggleRowSelection(row)"
          @sort-change="handleSortChange"
          @selection-change="handleSelectionChange"
          @filter-change="handleFilterChange"
          border
          stripe
          tooltip-effect="dark">
          <el-table-column
            width="40"
            type="selection"
            align="center">
          </el-table-column>
          <mes-col-comp
            :key="index"
            v-for="(column, index) in columnList"
            :column="column">
          </mes-col-comp>
          <el-table-column
            label="操作"
            show-overflow-tooltip
            :resizable="false"
            align="center"
            fixed="right"
            min-width="80">
            <template slot-scope="scope">
              <div class="operation">
                <el-tooltip class="item" effect="dark" content="操作记录" :open-delay="openDelay" placement="bottom">
                  <el-button type="text" size="small" @click.stop="handleViewLog(scope.row)">
                    <i class="iconfont icon-td_check"></i>
                  </el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="下载" :open-delay="openDelay" placement="bottom">
                  <el-button type="text" size="small">
                    <i class="iconfont icon-download"
                       @click.stop="$downByBlob(scope.row.uploadHistoryId, scope.row.fileName)"></i>
                  </el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="预览" :open-delay="openDelay" placement="bottom">
                  <el-button type="text" size="small">
                    <i @click.stop="$previewFile(scope.row.uploadHistoryId)"
                       v-if="$checkPreviewName(scope.row.fileName)&&$checkPreviewSize(scope.row.fileSize)"
                       class="iconfont icon-chakanyulan"></i>
                    <i v-else class="iconfont icon-chakanyulan disicon"></i>
                  </el-button>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <mes-pagination
        @updateData="init"
        :currentPage="listData.pageNo"
        :total="listData.count || 0"
        :pageSize="listData.pageSize">
      </mes-pagination>
    </div>
    <!--新增周计划弹窗-->
    <week-plan-add ref="WeekPlanAdd" @submitSuccess="init" :isNewPlan="isNewPlan" :planStatus="curWeekPlanStatusValue"></week-plan-add>
    <!--日志详情-->
    <log-detail ref="logDetail"></log-detail>
  </div>
</template>

<script>
import computed from './tableColumns'
import FileIcon from '@/components/FileIcon'
import WeekPlanAdd from './components/WeekPlayAdd'
import {WeekPlanStatus} from '@/components/mesDict'

export default {
  name: 'Planner',
  components: {
    FileIcon,
    WeekPlanAdd,
    WeekPlanStatus
  },
  data() {
    return {
      pickerOptions: {},
      isLoading: false, // 数据加载中
      searchData: {}, // 查询参数
      sortData: {}, // 排序对象
      searchCount: NaN, // 搜索结果数
      selectItems: [], // 选中的数据,
      dialogVisible: false, // 新增/修改周计划弹窗
      dialogTitle: '', // 新增/修改周计划弹窗title
      dialogViewLog: false, // 查看日志
      ViewLogName: '', // 查看日志上显示的周计划名称
      fileName: '', // 文件的名称
      step: 1,
      pickedDate: this.moment().format('YYYY-MM-DD 00:00:00'),
      formData: {},
      uploadFileData: null,
      rules: {
        fileSecretLevelValueRef: [
          {required: true, message: '请选择文档密级', trigger: 'change'}
        ],
        name: [
          {required: true, message: '请输入周计划名称', trigger: 'change'},
        ],
        weekPlanDate: [
          {required: true, message: '请选择周计划时间', trigger: 'change'}
        ],
        fileName: [
          {required: true, message: '请选择文件', trigger: 'change'}
        ]
      },
      isNewPlan: true, // 是否要添加新的周计划
      curWeekPlanStatusValue: '', // 当前周计划状态
    }
  },
  mounted() {
    this.init()
  },
  computed: computed,
  methods: {
    // 列筛选回调
    handleFilterChange(filters) {
      let filterParams = this.$filterParams(this.tableColConfig, filters)
      Object.assign(this.searchData, filterParams.search)
      this.sortData = filterParams.order // 排序对象
      this.$refs.tableNode.clearSort()
      this.init()
    },
    // 日期选择的change事件
    _currentDate(val) {
      let startDate = this.moment(val[0]).format('YYYY-MM-DD')
      let endDate = this.moment(val[1]).format('YYYY-MM-DD')
      let week = this.moment(val[0]).format('W')
      let strConnect = '第' + week + '周' + ' ' + startDate + '~' + endDate
      if (val) {
        this.formData.name = strConnect
      }
    },
    // 初始化方法
    init(page) {
      this.isLoading = true
      let postData = {
        method: 'post',
        url: 'a/weekplan/weekPlan/list',
        module: 'planner',
        data: {
          pageNo: page ? page.pageNo : 1,
          pageSize: page ? page.pageSize : this.listData.pageSize || 15
        },
        state: 'listData'
      }
      if (Object.keys(this.searchData).length > 0) {
        postData.data.query = this.searchData
      }
      if (Object.keys(this.sortData).length > 0) {
        postData.data.sort = this.sortData
      }
      this.$store.dispatch('saveData', postData).then((res) => {
        if (Object.keys(this.searchData).length > 0) {
          this.searchResult = res.count
        } else {
          this.searchResult = NaN
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    // 查询
    _info(data) {
      this.searchData = data
      this.sortData = {}
      this.$refs.tableNode.clearSort()
      this.$refs.tableNode.clearFilter()
      this.init()
    },
    // 排序方法
    handleSortChange(e) {
      this.sortData = this.$getSortOrder(e)
      this.init()
    },
    // 刷新方法
    refresh() {
      this.searchData = {} // 查询参数
      this.sortData = {} // 排序对象
      this.$refs.mesSearch.clean()
      this.$refs.tableNode.clearSort()
      this.$refs.tableNode.clearFilter()
      this.selectItems = []
      this.init()
    },
    // 选中方法
    handleSelectionChange(val) {
      this.selectItems = val
    },
    // 新增按钮
    handleAddPlan() {
      this.$refs.WeekPlanAdd.openDialog()
      this.isNewPlan = true
    },
    // 修改按钮（草稿、已退回、已确认可进行修改）
    handUpdate() {
      if (!this.selectItems.length) {
        return this.tipMessage('error', '请选择一条周计划数据')
      }
      if (this.eidtBtnStatus.status) return
      this.$refs.WeekPlanAdd.openDialog(this.selectItems)
      this.isNewPlan = false
      this.curWeekPlanStatusValue = this.selectItems[0].weekPlanStatusValue || undefined
    },
    // 删除提示信息
    handleDelete() {
      if (!this.selectItems.length) {
        return this.tipMessage('error', '请选择一条周计划数据')
      }
      if (this.deleteBtnStatus.status) return
      this.confirmDelete().then(() => {
        this.deleteItems()
      })
    },
    // 删除
    deleteItems() {
      let postData = {
        method: 'post',
        url: 'a/weekplan/weekPlan/deleteBatch2',
        module: 'planner',
        data: {
          weekPlanList: this.selectItems
        },
        noCommit: true
      }
      this.$store.dispatch('saveData', postData).then(() => {
        this.tipMessage('success', '删除成功')
        this.dialogVisible = false
        this.init(this.listData)
      })
    },
    // 提交的方法
    handleSubmit() {
      if (!this.selectItems.length) {
        return this.tipMessage('error', '请选择一条周计划数据')
      }
      if (this.submitBtnStatus.status) return
      this.mesConfirm('选中的记录将被提交, 是否继续?', '提交提示').then(() => {
        let postData = {
          method: 'post',
          url: 'a/weekplan/weekPlan/changePlanStatus',
          module: 'planner',
          data: {
            changeType: 'Unconfirmed',
            weekplanList: this.selectItems
          },
          noCommit: true
        }
        this.$store.dispatch('saveData', postData).then(() => {
          this.tipMessage('success', '提交成功')
          this.dialogVisible = false
          this.init(this.listData)
        })
      })
      // }
    },
    // 查看日志
    handleViewLog(row) {
      this.$refs.logDetail.openDialog({
        title: '周计划名称',
        name: row.name,
        url: 'a/weekplanstatuschangehistory/weekPlanStatusChangeHistory/list',
        query: {
          weekPlanId: row.id
        }
      })
    },
    /**
     * 提示
     * @param str 提示内容
     */
    tipMessage(type, str) {
      this.$message({
        type: type,
        message: str,
        duration: 1500,
        center: true
      })
    }
  }
}
</script>
<style lang="less">
  .weekly_plan_manage_planner {
    .file_btn {
      width: 100px;
    }

    .uploadFileName {
      .el-input__inner {
        border: none !important;
      }
    }

    .file_cell {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .input_wapper {
      .el-range-editor--small {
        .el-range__icon {
          color: #00b4ff;
          font-weight: 900;
        }
      }
    }

    .el-form-item {
      margin-bottom: 20px;

      .el-form-item__label {
        padding: 0;
        line-height: 32px;
      }

      .el-form-item__content {
        line-height: 32px;
      }
    }
  }
</style>
<style lang="less" scoped>
  .weekly_plan_manage_planner {
    .el-upload.el-upload--text {
      display: flex;
    }

    .el-upload.el-upload--text span {
      display: inline-block;
      width: 200px;
      text-align: left;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      vertical-align: middle;
    }

    .operation {
      color: #00b4ff;
      display: flex;
      justify-content: space-around;

      .iconfont {
        cursor: pointer;

        &.disicon {
          cursor: not-allowed;
          color: #a1b1c1;
        }
      }
    }

    .el-date-editor.el-range-editor.el-input__inner.el-date-editor--daterange {
      width: 100%
    }

    .uploadFileName {
      flex: 1;
      line-height: 32px;
    }

    .el-dialog__body {
      .center_msg {
        padding: 20px 0;
        font-size: 14px;
        color: #333;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;

        .el-icon-loading {
          font-size: 30px;
          margin-right: 10px;
          color: #00b4ff;
        }
      }

    }
  }
</style>
